package edu.nwpu.service.impl;

import edu.nwpu.dao.*;
import edu.nwpu.pojo.*;
import edu.nwpu.service.OrderService;
import edu.nwpu.utils.OrderUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service("orderService")
public class OrderServiceImpl implements OrderService {

    @Resource
    private OrderMapper orderMapper;
    @Resource
    private FoodCountMapper foodCountMapper;
    @Resource
    private FoodMapper foodMapper;

    @Override
    public Order findOrderById(Long id) {
        return orderMapper.selectOrderByOrderId(id);
    }

    @Override
    public List<Order> findUserOrders(Long userId) {
        return orderMapper.selectOrdersByUserId(userId);
    }

    @Override
    public List<Order> findAllOrders() {
        return orderMapper.selectAllOrders();
    }

    /**
     * 提交order到后台，事务管理
     *
     * @param order: order
     *
     * @return java.lang.String 反馈错误信息，正确提交返回null
     */
    @Override  @Transactional
    public String postOrder(Order order) {
        if (!OrderUtil.checkOrder(order)) {
            return "订单缺少必要的数据";
        }
        order.setStatusCode(OrderUtil.WAIT_PAY);
        order.setOrderTime(new Date());
        Long rows = orderMapper.insertOrder(order);
        double amount = 0.0;
        for (FoodCount foodCount : order.getFoods()) {
            foodCount.setOrderId(order.getId());
            if (foodCount.getNum() < 1) {
                throw new RuntimeException("菜品数量不能小于1");
            }
            foodCountMapper.insertFoodCount(foodCount);
            amount += foodCount.getNum() * foodMapper.selectFoodPriceByFoodId(foodCount.getFood().getId());
        }
        orderMapper.updateAmountByOrderId(order.getId(), amount);
        return null;
    }

    @Override
    public boolean deleteOrder(Long id) {
        return changeOrderStatus(id, OrderUtil.DELETED);
    }

    @Override
    public boolean cancelOrder(Long id) {
        return changeOrderStatus(id, OrderUtil.CANCELED);
    }

    @Override
    public boolean payOrder(Long id) {
        return changeOrderStatus(id, OrderUtil.PAYED);
    }

    @Override
    public boolean takeOrder(Long id) {
        return changeOrderStatus(id, OrderUtil.TAKEN);
    }

    @Override
    public boolean deliveringOrder(Long id) {
        return changeOrderStatus(id, OrderUtil.DELIVERING);
    }

    @Override
    public boolean deliveredOrder(Long id) {
        return changeOrderStatus(id, OrderUtil.DELIVERED);
    }

    /**
     * 更改order的状态码
     *
     * @param orderId: 订单id
     * @param newStatusCode: 新的状态码
     *
     * @return boolean 是否更改成功
     */
    private boolean changeOrderStatus(Long orderId, int newStatusCode) {
        Order order = orderMapper.selectOrderByOrderId(orderId);
        if (order == null){
            return false;
        }
        order.setStatusCode(newStatusCode);
        return orderMapper.updateOrder(order) == 1;
    }

}
